package com.nena.util;

public class Util {
	
	public static boolean isValidInteger(String num){
		
		if(num != null){
			try{
				Integer.parseInt(num);
				return true;
			}catch(NumberFormatException e){
				return false;
			}
		}else{
			return false;
		}
	}
	/**
	 * Returns true if string contains decimal digit.
	 * False otherwise. Never throws any exceptions.
	 * 
	 * @param str
	 * @return
	 */
	public static boolean containsDigit(String str){
		if (str == null ){
			return false;
		}else{
			for(int i=0; i < str.length(); i++){			
				if(Character.isDigit(str.charAt(i))){
					return true;
				}
			}
			return false;
		}
		
	}
	
	/**
	 * Returns hexadecimal representation of given decimal number.
	 * In case parameter is not valid decimal number null is returned.
	 * Never throws exception.
	 * 
	 * @param decNum
	 * @return
	 */
	public static String numToHex(String decNum){
		if ((decNum == null)||(decNum == "")){
			return null;
		}
		
		if(isValidInteger(decNum)){
			int i = Integer.parseInt(decNum);
		    String hex = Integer.toHexString(i);
		    return hex.toUpperCase();
		}else{
			return null;
		}
	    
	}
	
}
